package com.ruoyi.job.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.base.datam.StStationB;
import com.ruoyi.common.core.domain.business.waterManagement.*;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.job.service.*;
import com.ruoyi.job.service.impl.StStationBServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 水量定时任务调度测试
 *
 * @author hwd
 */
@Slf4j
@Component("waterYieldTask")
public class WaterYieldTask
{
    @Autowired
    private IStStationRService stStationRService;

    @Autowired
    private IStStationBService stStationBService;

    @Autowired
    private IWrStatvolHService wrStatvolHService;

    @Autowired
    private IWrStationRService wrStatvolRService;

    @Autowired
    private IWrStatvolDService wrStatvolDService;

    @Autowired
    private IWrStatvolMService wrStatvolMService;

    @Autowired
    private IWrStatvolYService wrStatvolYService;

    @Autowired
    private IWiOrgvolDService wiOrgvolDService;

    @Autowired
    private IWiOrgvolMService wiOrgvolMService;

    @Autowired
    private IWiOrgvolYService wiOrgvolYService;

    //获取测站时段水量表,测站日水量，测站月水量，测站年水量
    public void  saveWaterYield() {
        //查询所有测站代码
        StStationB stStationB = new StStationB();
        stStationB.setStatType("2");
        List<StStationB> stationBList = stStationBService.selectStStationBList(stStationB);
        for (StStationB s : stationBList
        ) {
            //查询小时库里最新数据
            WrStatvolH wrStatvolH = wrStatvolHService.getOne(new QueryWrapper<WrStatvolH>().select("top 1 *").eq("stat_cd", s.getStatCd()).orderByDesc("stat_hour"));//先查询测站最新的一条在什么时间
            if (wrStatvolH == null) {
                wrStatvolH = new WrStatvolH();
                Date fromDate = new Date();
                Calendar c = Calendar.getInstance();
                c.setTime(fromDate);
                c.set(Calendar.YEAR, 2011);
                fromDate = c.getTime();
                wrStatvolH.setStatHour(fromDate);//设置为2011年
                wrStatvolH.setHourVol(new BigDecimal(0));
                wrStatvolH.setHourSum(new BigDecimal(0));
            }
            Date zeroDate = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.SECOND, 0); //这是将当天的【秒】设置为0
            calendar.set(Calendar.MINUTE, 0); //这是将当天的【分】设置为0
            calendar.set(Calendar.HOUR_OF_DAY, 0); //这是将当天的【时】设置为0
            calendar.set(Calendar.MILLISECOND, 0);//这是将当天的【毫秒】设置为0
            zeroDate = calendar.getTime();
            //查询比库里时间最新时间大的数据
//            LambdaQueryWrapper<StStationR> lambdaQueryWrapper =
//                    new LambdaQueryWrapper<>();
//            lambdaQueryWrapper.eq(StStationR::getStatCd,s.getStatCd()).ge(StStationR::getChkTime,wrStatvolH.getStatHour()).lt(StStationR::getChkTime,zeroDate);
            //查询出各小时平均水位 时间减1秒(整点的时间归到上一个小时)
            List<StStationR> stationRList = stStationRService.selStationRHourAvgCurLevel(s.getStatCd(), wrStatvolH.getStatHour(), zeroDate);
            for (StStationR sr : stationRList
            ) {
                //查询本小时的累计流量-上一个小时的累计流量=时段水量
                WrStatvolH wrStatvolH1 = new WrStatvolH();
                wrStatvolH1.setStatCd(s.getStatCd());
                wrStatvolH1.setHourLevel(sr.getCurLevel());
                Date lastChkTime = getChkTime(sr.getChkTime(), -1);
                //查询当前小时和上一个小时的累计流量/时间数据 sumFlow chKTime lastChkTime lastSumFlow
                StStationR stationR = stStationRService.selStationRHourVol(s.getStatCd(), sr.getChkTime(), lastChkTime);
                if (stationR != null) {
                    BigDecimal hourVol = new BigDecimal("0.0");
                    BigDecimal hourFlow = new BigDecimal("0.0");
                    if (stationR.getLastChkTime() == null) {  //如果之前没数据，就直接添加水量
                        hourVol = stationR.getSumFlow().divide(new BigDecimal(10000), 3, BigDecimal.ROUND_HALF_UP);
                        hourFlow = hourVol.multiply(new BigDecimal(10000));
                        BigDecimal d = new BigDecimal("3600");
                        hourFlow = hourFlow.divide(d, 3, BigDecimal.ROUND_HALF_UP);
                    } else if (stationR.getLastChkTime() != null) {    //如果之前有数据，就按时间减去
                        //转换为毫秒
                        //上一小时数据
                        long stLastChkTime = stationR.getLastChkTime().getTime();
                        //当前小时数据
                        long stChkTime = stationR.getChkTime().getTime();
                        //当前整点时间
                        long time = sr.getChkTime().getTime();
                        //上一小时整点时间
                        long lasttime = lastChkTime.getTime();
                        if (stLastChkTime == lasttime) {//如果上一个小时最新数据正好是整点，说明直接套公示就行
                            hourVol = (stationR.getSumFlow().subtract(stationR.getLastSumFlow())).divide(new BigDecimal(10000), 3, BigDecimal.ROUND_HALF_UP);
                            if (hourVol.compareTo(new BigDecimal("0.0"))<=0) {
                                hourFlow = new BigDecimal("0.0");
                            }else {
                                hourFlow = hourVol.multiply(new BigDecimal(10000)).divide(calLastedTime(stationR.getChkTime(), stationR.getLastChkTime()), 3, BigDecimal.ROUND_HALF_UP);
                            }
                        } else //如果小于上一小时整点时间，就要按时间拆分计算水量
                        {
                            BigDecimal stLastChkTimeBig = new BigDecimal(stLastChkTime);
                            BigDecimal stChkTimeBig = new BigDecimal(stChkTime);
                            BigDecimal timeBig = new BigDecimal(time);
                            hourVol = (stationR.getSumFlow().subtract(stationR.getLastSumFlow())).divide(new BigDecimal(10000), 3, BigDecimal.ROUND_HALF_UP);//当前减去上一次累计水量
                            hourVol = hourVol.divide(stChkTimeBig.subtract(stLastChkTimeBig), 3, BigDecimal.ROUND_HALF_UP).multiply(stChkTimeBig.subtract(timeBig));//时段累计水量/相差时间*当前小时间隔时间
                            if (hourVol.compareTo(new BigDecimal("0.0"))<=0){
                                hourFlow=new BigDecimal("0.0");
                            }else {
                                hourFlow = hourVol.multiply(new BigDecimal(10000)).divide(calLastedTime(stationR.getChkTime(), sr.getChkTime()), 3, BigDecimal.ROUND_HALF_UP);//当前小时间隔时间
                            }
                        }

                    }
                    wrStatvolH1.setStatHour(sr.getChkTime());
                    wrStatvolH1.setHourVol(hourVol);
                    wrStatvolH1.setHourFlow(hourFlow);
                    wrStatvolH1.setHourSum(wrStatvolH.getHourSum().add(hourVol));
                    wrStatvolHService.insertOrUpdateWrStatvolH(wrStatvolH1);
//                    //保存测站日水量
//                    WrStatvolH wrStatvolD = wrStatvolHService.countDayByWrStatvolH(wrStatvolH1);
//                    wrStatvolD.setStatCd(wrStatvolH1.getStatCd());
//                    wrStatvolDService.insertOrUpdateWrStatvolD(wrStatvolD);
//                    //通过测站统计水管机构日水量
//                    WrStatvolD wiOrgVolD = wrStatvolDService.countWiOrgVolDByWrStatvolD(wrStatvolH1.getStatCd(), wrStatvolH1.getStatHour());
//                    if (wiOrgVolD != null) {
//                        //保存水管机构日水量表
//                        wiOrgvolDService.insertOrUpdateWiOrgvolD(wiOrgVolD);
//                    }
//
//                    //保存测站月水量
//                    WrStatvolD wrStatvolM = wrStatvolDService.countMonthByWrStatvolD(wrStatvolH1.getStatCd(), wrStatvolH1.getStatHour());
//                    wrStatvolM.setStatDay(wrStatvolH1.getStatHour());
//                    wrStatvolM.setStatCd(wrStatvolH1.getStatCd());
//                    wrStatvolMService.insertOrUpdateWrStatvolM(wrStatvolM);
//                    //通过测站月水表统计水管机构月水量
//                    WrStatvolM wiOrgVolM = wrStatvolMService.countWiOrgVolMByWrStatvolM(wrStatvolH1.getStatCd(), wrStatvolH1.getStatHour());
//                    if (wiOrgVolM != null) {
//                        //保存水管机构月水量表
//                        wiOrgvolMService.insertOrUpdateWiOrgvolM(wiOrgVolM);
//                    }
//
//                    //保存测站年水量
//                    WrStatvolM wrStatvolY = wrStatvolMService.countYearByWrStatvolM(wrStatvolH1.getStatCd(), wrStatvolH1.getStatHour());
//                    wrStatvolY.setStatCd(wrStatvolH1.getStatCd());
//                    wrStatvolYService.insertOrUpdateWrStatvolY(wrStatvolY, wrStatvolH1.getStatHour());
//                    //通过测站年水表统计水管机构年水量
//                    WrStatvolY wiOrgVolY = wrStatvolYService.countWiOrgVolYByWrStatvolY(wrStatvolH1.getStatCd(), wrStatvolH1.getStatHour());
//                    if (wiOrgVolY != null) {
//                        //保存水管机构月水量表
//                        wiOrgvolYService.insertOrUpdateWiOrgvolY(wiOrgVolY);
//                    }


                }
                //System.out.println(wrStatvolH1.toString());
            }

        }
    }





    //获取测站时段水量表,
    public void  saveWaterYieldTypeSl(String statType){
        //查询所有测站代码
        StStationB  stStationB= new StStationB();
        stStationB.setStatType(statType);
        List<StStationB> stationBList = stStationBService.selectStStationBList(stStationB);
        for (StStationB s:stationBList
        ) {
            //查询小时库里最新数据
            WrStatvolH wrStatvolH=  wrStatvolHService.getOne(new QueryWrapper<WrStatvolH>().select("top 1 *").eq("stat_cd", s.getStatCd()).orderByDesc("stat_hour"));//先查询测站最新的一条在什么时间
            if (wrStatvolH==null){
                wrStatvolH =new WrStatvolH();
                Date fromDate=new Date();
                Calendar c = Calendar.getInstance();
                c.setTime(fromDate);
                c.set(Calendar.YEAR,2011);
                fromDate = c.getTime();
                wrStatvolH.setStatHour(fromDate);//设置为2011年
                wrStatvolH.setHourSum(new BigDecimal(0));
            }
            Date zeroDate=new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.SECOND,0); //这是将当天的【秒】设置为0
            calendar.set(Calendar.MINUTE,0); //这是将当天的【分】设置为0
            calendar.set(Calendar.HOUR_OF_DAY,0); //这是将当天的【时】设置为0
            calendar.set(Calendar.MILLISECOND,0);//这是将当天的【毫秒】设置为0
            zeroDate = calendar.getTime();
            //查询比库里时间最新时间大的数据
//            LambdaQueryWrapper<StStationR> lambdaQueryWrapper =
//                    new LambdaQueryWrapper<>();
//            lambdaQueryWrapper.eq(StStationR::getStatCd,s.getStatCd()).ge(StStationR::getChkTime,wrStatvolH.getStatHour()).lt(StStationR::getChkTime,zeroDate);
            //查询出各小时平均水位 时间减1秒(整点的时间归到上一个小时)
            List<WrStationR> wrStationRList =wrStatvolRService.selWrStationRHourAvgCurFlow(s.getStatCd(),wrStatvolH.getStatHour(),zeroDate);
            for (WrStationR  wr:wrStationRList
            ) {
                //查询本小时的累计流量-上一个小时的累计流量=时段水量
                WrStatvolH wrStatvolH1 =new WrStatvolH();
                wrStatvolH1.setStatCd(s.getStatCd());
                Date lastChkTime = getChkTime(wr.getChkTime(),-1);
                //查询当前小时和上一个小时的累计流量/时间数据 sumFlow chKTime lastChkTime lastSumFlow
                WrStationR wrStationR =wrStatvolRService.selWrStationRHourVol(s.getStatCd(),wr.getChkTime(),lastChkTime);
                if (wrStationR!=null){
                    BigDecimal hourVol= new BigDecimal("0.0");
                    BigDecimal hourFlow=new BigDecimal("0.0");
                    if (wrStationR.getLastChkTime()==null){  //如果之前没数据，就直接添加水量
                        hourVol =  wrStationR.getSumFlow().divide(new BigDecimal(10000),3,BigDecimal.ROUND_HALF_UP);
                        if (hourVol.compareTo(new BigDecimal("0.0"))<=0) {
                            hourFlow = new BigDecimal("0.0");
                        }else {
                            hourFlow = hourVol.multiply(new BigDecimal(10000));
                            BigDecimal d =new BigDecimal("3600");
                            hourFlow=hourFlow.divide(d,3,BigDecimal.ROUND_HALF_UP);
                        }

                    }else  if (wrStationR.getLastChkTime()!=null) {    //如果之前有数据，就按时间减去
                        //转换为毫秒
                        //上一小时数据
                        long stLastChkTime = wrStationR.getLastChkTime().getTime();
                        //当前小时数据
                        long stChkTime =wrStationR.getChkTime().getTime();
                        //当前整点时间
                        long time = wr.getChkTime().getTime();
                        //上一小时整点时间
                        long lasttime =lastChkTime.getTime();
                        if ( stLastChkTime==lasttime) {//如果上一个小时最新数据正好是整点，说明直接套公示就行
                            hourVol = (wrStationR.getSumFlow().subtract(wrStationR.getLastSumFlow())).divide(new BigDecimal(10000),3,BigDecimal.ROUND_HALF_UP);
                            if (hourVol.compareTo(new BigDecimal("0.0"))<=0) {
                                hourFlow = new BigDecimal("0.0");
                            }else {
                                hourFlow = hourVol.multiply(new BigDecimal(10000)).divide(calLastedTime(wrStationR.getChkTime(), wrStationR.getLastChkTime()), 3, BigDecimal.ROUND_HALF_UP);
                            }
                        }else //如果小于上一小时整点时间，就要按时间拆分计算水量
                        {
                            BigDecimal stLastChkTimeBig =new BigDecimal(stLastChkTime);
                            BigDecimal stChkTimeBig =new BigDecimal(stChkTime);
                            BigDecimal timeBig =new BigDecimal(time);
                            hourVol = (wrStationR.getSumFlow().subtract(wrStationR.getLastSumFlow())).divide(new BigDecimal(10000),3,BigDecimal.ROUND_HALF_UP);//当前减去上一次累计水量
                            hourVol =hourVol.divide(stChkTimeBig.subtract(stLastChkTimeBig),3,BigDecimal.ROUND_HALF_UP).multiply(stChkTimeBig.subtract(timeBig));//时段累计水量/相差时间*当前小时间隔时间
                            if (hourVol.compareTo(new BigDecimal(0))<=0){
                                hourFlow=new BigDecimal(0);
                            }else {
                                hourFlow = hourVol.multiply(new BigDecimal(10000)).divide(calLastedTime(wrStationR.getChkTime(), wr.getChkTime()),3,BigDecimal.ROUND_HALF_UP);//当前小时间隔时间

                            }
                        }
                    }
                    wrStatvolH1.setStatHour(wr.getChkTime());
                    wrStatvolH1.setHourVol(hourVol);
                    wrStatvolH1.setHourFlow(hourFlow);
                    wrStatvolH1.setHourSum(wrStatvolH.getHourSum().add(hourVol));
                    wrStatvolHService.insertOrUpdateWrStatvolH(wrStatvolH1);
//                        //保存测站日水量
//                        WrStatvolH wrStatvolD=  wrStatvolHService.countDayByWrStatvolH(wrStatvolH1);
//                        wrStatvolD.setStatCd(wrStatvolH1.getStatCd());
//                        wrStatvolDService.insertOrUpdateWrStatvolD(wrStatvolD);
//                        //通过测站统计水管机构日水量
//                        WrStatvolD wiOrgVolD = wrStatvolDService.countWiOrgVolDByWrStatvolD(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                        if (wiOrgVolD!=null){
//                            //保存水管机构日水量表
//                            wiOrgvolDService.insertOrUpdateWiOrgvolD(wiOrgVolD);
//                        }
//
//                        //保存测站月水量
//                        WrStatvolD wrStatvolM=  wrStatvolDService.countMonthByWrStatvolD(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                        wrStatvolM.setStatDay(wrStatvolH1.getStatHour());
//                        wrStatvolM.setStatCd(wrStatvolH1.getStatCd());
//                        wrStatvolMService.insertOrUpdateWrStatvolM(wrStatvolM);
//                        //通过测站月水表统计水管机构月水量
//                        WrStatvolM wiOrgVolM = wrStatvolMService.countWiOrgVolMByWrStatvolM(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                        if (wiOrgVolM!=null){
//                            //保存水管机构月水量表
//                            wiOrgvolMService.insertOrUpdateWiOrgvolM(wiOrgVolM);
//                        }
//
//                        //保存测站年水量
//                        WrStatvolM wrStatvolY = wrStatvolMService.countYearByWrStatvolM(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                        wrStatvolY.setStatCd(wrStatvolH1.getStatCd());
//                        wrStatvolYService.insertOrUpdateWrStatvolY(wrStatvolY,wrStatvolH1.getStatHour());
//                        //通过测站年水表统计水管机构年水量
//                        WrStatvolY wiOrgVolY = wrStatvolYService.countWiOrgVolYByWrStatvolY(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                        if (wiOrgVolY!=null){
//                            //保存水管机构月水量表
//                            wiOrgvolYService.insertOrUpdateWiOrgvolY(wiOrgVolY);
//                        }


                }
                //System.out.println(wrStatvolH1.toString());
            }

        }







    }

    private Date getChkTime (Date chktime,int a){
        Calendar c = Calendar.getInstance();
        c.setTime(chktime);
        c.add(Calendar.HOUR,a);   //利用Calendar 实现 Date日期加减小时
        return  c.getTime();
    }

    public  BigDecimal calLastedTime(Date chkTime,Date minChkTime) {
        long a = chkTime.getTime();
        long b = minChkTime.getTime();
        BigDecimal c =new BigDecimal ((int)((a - b) / 1000));
        return c;
    }
}


//package com.ruoyi.job.task;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.ruoyi.common.core.domain.base.datam.StStationB;
//import com.ruoyi.common.core.domain.business.waterManagement.*;
//import com.ruoyi.common.core.utils.StringUtils;
//import com.ruoyi.job.service.*;
//import com.ruoyi.job.service.impl.StStationBServiceImpl;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.math.BigDecimal;
//import java.util.Calendar;
//import java.util.Date;
//import java.util.List;
//
///**
// * 水量定时任务调度测试
// *
// * @author hwd
// */
//@Slf4j
//@Component("waterYieldTask")
//public class WaterYieldTask
//{
//    @Autowired
//    private IStStationRService stStationRService;
//
//    @Autowired
//    private IStStationBService stStationBService;
//
//    @Autowired
//    private IWrStatvolHService wrStatvolHService;
//
//    @Autowired
//    private IWrStatvolDService wrStatvolDService;
//
//    @Autowired
//    private IWrStatvolMService wrStatvolMService;
//
//    @Autowired
//    private IWrStatvolYService wrStatvolYService;
//
//    @Autowired
//    private IWiOrgvolDService wiOrgvolDService;
//
//    @Autowired
//    private IWiOrgvolMService wiOrgvolMService;
//
//    @Autowired
//    private IWiOrgvolYService wiOrgvolYService;
//
//    //获取测站时段水量表,测站日水量，测站月水量，测站年水量
//    public void  saveWaterYield(){
//        //查询所有测站代码
//        StStationB  stStationB= new StStationB();
//        stStationB.setStatType("2");
//        List<StStationB> stationBList = stStationBService.selectStStationBList(stStationB);
//        for (StStationB s:stationBList
//             ) {
//            //查询小时库里最新数据
//          WrStatvolH wrStatvolH=  wrStatvolHService.getOne(new QueryWrapper<WrStatvolH>().select("top 1 *").eq("stat_cd", s.getStatCd()).orderByDesc("stat_hour"));//先查询测站最新的一条在什么时间
//           if (wrStatvolH==null){
//               wrStatvolH =new WrStatvolH();
//               Date fromDate=new Date();
//               Calendar c = Calendar.getInstance();
//               c.setTime(fromDate);
//               c.set(Calendar.YEAR,2011);
//               fromDate = c.getTime();
//               wrStatvolH.setStatHour(fromDate);//设置为2011年
//               wrStatvolH.setHourVol(new BigDecimal(0));
//               wrStatvolH.setHourSum(new BigDecimal(0));
//           }
//            Date zeroDate=new Date();
//            Calendar calendar = Calendar.getInstance();
//            calendar.set(Calendar.SECOND,0); //这是将当天的【秒】设置为0
//            calendar.set(Calendar.MINUTE,0); //这是将当天的【分】设置为0
//            calendar.set(Calendar.HOUR_OF_DAY,0); //这是将当天的【时】设置为0
//            calendar.set(Calendar.MILLISECOND,0);//这是将当天的【毫秒】设置为0
//            zeroDate = calendar.getTime();
//            //查询比库里时间最新时间大的数据
////            LambdaQueryWrapper<StStationR> lambdaQueryWrapper =
////                    new LambdaQueryWrapper<>();
////            lambdaQueryWrapper.eq(StStationR::getStatCd,s.getStatCd()).ge(StStationR::getChkTime,wrStatvolH.getStatHour()).lt(StStationR::getChkTime,zeroDate);
//            //查询出各小时平均水位 时间减1秒(整点的时间归到上一个小时)
//            List<StStationR> stationRList =stStationRService.selStationRHourAvgCurLevel(s.getStatCd(),wrStatvolH.getStatHour(),zeroDate);
//            for (StStationR  sr:stationRList
//                 ) {
//                //查询本小时的累计流量-上一个小时的累计流量=时段水量
//                WrStatvolH wrStatvolH1 =new WrStatvolH();
//                wrStatvolH1.setStatCd(s.getStatCd());
//                wrStatvolH1.setHourLevel(sr.getCurLevel());
//                Date lastChkTime = getChkTime(sr.getChkTime(),-1);
//                //查询当前小时和上一个小时的累计流量/时间数据 sumFlow chKTime lastChkTime lastSumFlow
//                StStationR stationR =stStationRService.selStationRHourVol(s.getStatCd(),sr.getChkTime(),lastChkTime);
//                if (stationR!=null){
//                    BigDecimal hourVol= new BigDecimal("0.0");
//                    BigDecimal hourFlow=new BigDecimal("0.0");
//                    if (stationR.getLastChkTime()==null){  //如果之前没数据，就直接添加水量
//                        hourVol =  stationR.getSumFlow().divide(new BigDecimal(10000),3,BigDecimal.ROUND_HALF_UP);
//                        hourFlow = hourVol.multiply(new BigDecimal(10000));
//                        BigDecimal d =new BigDecimal("3600");
//                        hourFlow=hourFlow.divide(d,3,BigDecimal.ROUND_HALF_UP);
//                    }else  if (stationR.getLastChkTime()!=null) {    //如果之前有数据，就按时间减去
//                        //转换为毫秒
//                        //上一小时数据
//                        long stLastChkTime = stationR.getLastChkTime().getTime();
//                        //当前小时数据
//                        long stChkTime =stationR.getChkTime().getTime();
//                        //当前整点时间
//                        long time = sr.getChkTime().getTime();
//                        //上一小时整点时间
//                        long lasttime =lastChkTime.getTime();
//                        if ( stLastChkTime==lasttime) {//如果上一个小时最新数据正好是整点，说明直接套公示就行
//                            hourVol = (stationR.getSumFlow().subtract(stationR.getLastSumFlow())).divide(new BigDecimal(10000),3,BigDecimal.ROUND_HALF_UP);
//                            hourFlow = hourVol.multiply(new BigDecimal(10000)).divide(calLastedTime(stationR.getChkTime(), stationR.getLastChkTime()),3,BigDecimal.ROUND_HALF_UP);
//                        }else //如果小于上一小时整点时间，就要按时间拆分计算水量
//                            {
//                            BigDecimal stLastChkTimeBig =new BigDecimal(stLastChkTime);
//                            BigDecimal stChkTimeBig =new BigDecimal(stChkTime);
//                            BigDecimal timeBig =new BigDecimal(time);
//                            hourVol = (stationR.getSumFlow().subtract(stationR.getLastSumFlow())).divide(new BigDecimal(10000),3,BigDecimal.ROUND_HALF_UP);//当前减去上一次累计水量
//                            hourVol =hourVol.divide(stChkTimeBig.subtract(stLastChkTimeBig),3,BigDecimal.ROUND_HALF_UP).multiply(stChkTimeBig.subtract(timeBig));//时段累计水量/相差时间*当前小时间隔时间
//                            hourFlow = hourVol.multiply(new BigDecimal(10000)).divide(calLastedTime(stationR.getChkTime(), sr.getChkTime()),3,BigDecimal.ROUND_HALF_UP);//当前小时间隔时间
//                        }
//                    }
//                    wrStatvolH1.setStatHour(sr.getChkTime());
//                    wrStatvolH1.setHourVol(hourVol);
//                    wrStatvolH1.setHourFlow(hourFlow);
//                    wrStatvolH1.setHourSum(wrStatvolH.getHourSum().add(hourVol));
//                    wrStatvolHService.insertOrUpdateWrStatvolH(wrStatvolH1);
//                    //保存测站日水量
//                   WrStatvolH wrStatvolD=  wrStatvolHService.countDayByWrStatvolH(wrStatvolH1);
//                    wrStatvolD.setStatCd(wrStatvolH1.getStatCd());
//                    wrStatvolDService.insertOrUpdateWrStatvolD(wrStatvolD);
//                    //通过测站统计水管机构日水量
//                    WrStatvolD wiOrgVolD = wrStatvolDService.countWiOrgVolDByWrStatvolD(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                    if (wiOrgVolD!=null){
//                        //保存水管机构日水量表
//                        wiOrgvolDService.insertOrUpdateWiOrgvolD(wiOrgVolD);
//                    }
//
//                    //保存测站月水量
//                  WrStatvolD wrStatvolM=  wrStatvolDService.countMonthByWrStatvolD(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                    wrStatvolM.setStatDay(wrStatvolH1.getStatHour());
//                    wrStatvolM.setStatCd(wrStatvolH1.getStatCd());
//                    wrStatvolMService.insertOrUpdateWrStatvolM(wrStatvolM);
//                    //通过测站月水表统计水管机构月水量
//                    WrStatvolM wiOrgVolM = wrStatvolMService.countWiOrgVolMByWrStatvolM(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                    if (wiOrgVolM!=null){
//                        //保存水管机构月水量表
//                        wiOrgvolMService.insertOrUpdateWiOrgvolM(wiOrgVolM);
//                    }
//
//                    //保存测站年水量
//                   WrStatvolM wrStatvolY = wrStatvolMService.countYearByWrStatvolM(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                    wrStatvolY.setStatCd(wrStatvolH1.getStatCd());
//                   wrStatvolYService.insertOrUpdateWrStatvolY(wrStatvolY,wrStatvolH1.getStatHour());
//                    //通过测站年水表统计水管机构年水量
//                    WrStatvolY wiOrgVolY = wrStatvolYService.countWiOrgVolYByWrStatvolY(wrStatvolH1.getStatCd(),wrStatvolH1.getStatHour());
//                    if (wiOrgVolY!=null){
//                        //保存水管机构月水量表
//                        wiOrgvolYService.insertOrUpdateWiOrgvolY(wiOrgVolY);
//                    }
//
//
//                }
//               //System.out.println(wrStatvolH1.toString());
//            }
//
//        }
//
//
//
//
//
//
//
//
//
//
//
//
//
//    }
//
//    private Date getChkTime (Date chktime,int a){
//        Calendar c = Calendar.getInstance();
//        c.setTime(chktime);
//        c.add(Calendar.HOUR,a);   //利用Calendar 实现 Date日期加减小时
//        return  c.getTime();
//    }
//
//    public  BigDecimal calLastedTime(Date chkTime,Date minChkTime) {
//        long a = chkTime.getTime();
//        long b = minChkTime.getTime();
//        BigDecimal c =new BigDecimal ((int)((a - b) / 1000));
//        return c;
//    }
//}
